home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Samples / Media / EffectEdit / Simple.fx < prev    next >
Encoding:
Text File  |  2002-11-15  |  4.2 KB  |  166 lines

  1. //
  2. // Simple Lighting Model
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Note: This effect file works with EffectEdit.
  6. //
  7.  
  8. string XFile = "tiger.x";   // model
  9. int    BCLR = 0xff202080;   // background
  10.  
  11. // light direction (view space)
  12. float3 lightDir <  string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};
  13.  
  14. // light intensity
  15. float4 I_a = { 0.1f, 0.1f, 0.1f, 1.0f };    // ambient
  16. float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
  17. float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  18.  
  19. // material reflectivity
  20. float4 k_a : MATERIALAMBIENT = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient
  21. float4 k_d : MATERIALDIFFUSE = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
  22. float4 k_s : MATERIALSPECULAR= { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  23. int    n   : MATERIALPOWER = 32;                            // power
  24.  
  25. // texture
  26. texture Tex0 < string name = "tiger.bmp"; >;
  27.  
  28. // transformations
  29. float4x4 World      : WORLD;
  30. float4x4 View       : VIEW;
  31. float4x4 Projection : PROJECTION;
  32.  
  33. struct VS_OUTPUT
  34. {
  35.     float4 Pos  : POSITION;
  36.     float4 Diff : COLOR0;
  37.     float4 Spec : COLOR1;
  38.     float2 Tex  : TEXCOORD0;
  39. };
  40.  
  41. VS_OUTPUT VS(
  42.     float3 Pos  : POSITION, 
  43.     float3 Norm : NORMAL, 
  44.     float2 Tex  : TEXCOORD0)
  45. {
  46.     VS_OUTPUT Out = (VS_OUTPUT)0;
  47.  
  48.     float3 L = -lightDir;
  49.  
  50.     float4x4 WorldView = mul(World, View);
  51.  
  52.     float3 P = mul(float4(Pos, 1), (float4x3)WorldView);  // position (view space)
  53.     float3 N = normalize(mul(Norm, (float3x3)WorldView)); // normal (view space)
  54.  
  55.     float3 R = normalize(2 * dot(N, L) * N - L);          // reflection vector (view space)
  56.     float3 V = -normalize(P);                             // view direction (view space)
  57.  
  58.     Out.Pos  = mul(float4(P, 1), Projection);             // position (projected)
  59.     Out.Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient
  60.     Out.Spec = I_s * k_s * pow(max(0, dot(R, V)), n/4);   // specular
  61.     Out.Tex  = Tex;                                       
  62.  
  63.     return Out;
  64. }
  65.  
  66. sampler Sampler = sampler_state
  67. {
  68.     Texture   = (Tex0);
  69.     MipFilter = LINEAR;
  70.     MinFilter = LINEAR;
  71.     MagFilter = LINEAR;
  72. };
  73.  
  74. float4 PS(
  75.     float4 Diff : COLOR0,
  76.     float4 Spec : COLOR1,
  77.     float2 Tex  : TEXCOORD0) : COLOR
  78. {
  79.     return tex2D(Sampler, Tex) * Diff + Spec;
  80. }
  81.  
  82. technique TNoShader
  83. {
  84.     pass P0
  85.     {
  86.         // transforms
  87.         WorldTransform[0]   = (World);
  88.         ViewTransform       = (View);
  89.         ProjectionTransform = (Projection);
  90.  
  91.         // material
  92.         MaterialAmbient  = (k_a); 
  93.         MaterialDiffuse  = (k_d); 
  94.         MaterialSpecular = (k_s); 
  95.         MaterialPower    = (n);
  96.         
  97.         // lighting
  98.         LightType[0]      = DIRECTIONAL;
  99.         LightAmbient[0]   = (I_a);
  100.         LightDiffuse[0]   = (I_d);
  101.         LightSpecular[0]  = (I_s); 
  102.         LightDirection[0] = (lightDir);
  103.         LightRange[0]     = 100000.0f;
  104.  
  105.         LightEnable[0] = TRUE;
  106.         Lighting       = TRUE;
  107.         SpecularEnable = TRUE;
  108.         
  109.         // samplers
  110.         Sampler[0] = (Sampler);
  111.         
  112.         // texture stages
  113.         ColorOp[0]   = MODULATE;
  114.         ColorArg1[0] = TEXTURE;
  115.         ColorArg2[0] = DIFFUSE;
  116.         AlphaOp[0]   = MODULATE;
  117.         AlphaArg1[0] = TEXTURE;
  118.         AlphaArg2[0] = DIFFUSE;
  119.  
  120.         ColorOp[1]   = DISABLE;
  121.         AlphaOp[1]   = DISABLE;
  122.  
  123.         // shaders
  124.         VertexShader = NULL;
  125.         PixelShader  = NULL;
  126.     }
  127. }
  128.  
  129. technique TVertexShaderOnly
  130. {
  131.     pass P0
  132.     {
  133.         // lighting
  134.         Lighting       = FALSE;
  135.         SpecularEnable = TRUE;
  136.  
  137.         // samplers
  138.         Sampler[0] = (Sampler);
  139.  
  140.         // texture stages
  141.         ColorOp[0]   = MODULATE;
  142.         ColorArg1[0] = TEXTURE;
  143.         ColorArg2[0] = DIFFUSE;
  144.         AlphaOp[0]   = MODULATE;
  145.         AlphaArg1[0] = TEXTURE;
  146.         AlphaArg2[0] = DIFFUSE;
  147.  
  148.         ColorOp[1]   = DISABLE;
  149.         AlphaOp[1]   = DISABLE;
  150.  
  151.         // shaders
  152.         VertexShader = compile vs_1_1 VS();
  153.         PixelShader  = NULL;
  154.     }
  155. }
  156.  
  157. technique TVertexAndPixelShader
  158. {
  159.     pass P0
  160.     {
  161.         // shaders
  162.         VertexShader = compile vs_1_1 VS();
  163.         PixelShader  = compile ps_1_1 PS();
  164.     }  
  165. }
  166.